#!/usr/bin/python
## Import gtk
import sys
try:  
    import pygtk  
    pygtk.require("2.0")  
except:  
    pass  
try:  
    import gtk  
    import gtk.glade  
except:  
    print("GTK Not Availible")
    sys.exit(1)

## Import Book
import AiCompareGui
sys.path.append( "../imageFeatures_classes" )
import AiBook

class AiGui:
    wTree = None

    def __init__( self ):
        self.book = None
        self.wTree = gtk.glade.XML( "bookGui.glade" )
        
        dictionary = { 
            "on_buttonBlobTrace_clicked" : self.blobTrace,
            "on_buttonLoadBook_clicked" : self.loadBook,
            "on_buttonNextPage_clicked" : self.nextPage,
            "on_buttonPrevPage_clicked" : self.prevPage,
            "on_buttonCompareBlobs_clicked" : self.compareBlobs,
            "on_buttonNLP_clicked" : self.nlp,
            "on_buttonEM_clicked" : self.em,
            "on_windowMain_destroy" : gtk.main_quit
        }
        
        self.wTree.signal_autoconnect( dictionary )
        self.checkButtons()
        self.mainWindow = self.wTree.get_widget("windowMain")
        self.mainWindow.show()

    ## Buttons
    def loadBook(self, widget):
        filter = gtk.FileFilter()
        filter.set_name("XML Files")
        filter.add_pattern("*.xml")

        chooser = gtk.FileChooserDialog( title="Open XML Book", \
                    action=gtk.FILE_CHOOSER_ACTION_OPEN, \
                    buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL, \
                    gtk.STOCK_OPEN, \
                    gtk.RESPONSE_OK) )
        chooser.set_default_response(gtk.RESPONSE_OK)
        chooser.add_filter(filter)

        response = chooser.run()
        if response == gtk.RESPONSE_OK:
            if __debug__:
                print "GUI: Loading", chooser.get_filename()
            self.book = AiBook.AiBook( chooser.get_filename() )
            self.loadPage(0)
            self.checkButtons()
            self.wTree.get_widget("buttonNextPage").set_sensitive(True)
            self.wTree.get_widget("buttonPrevPage").set_sensitive(False)

        chooser.destroy() 

    def loadPage(self, nr):
        buf = self.wTree.get_widget("textviewFeatures").get_buffer()
        buf.set_text("") #clear
        if nr < 1:
            ptext = "\nTitle: " + self.book.title
            pbuff = self.book.coverimage.asGtkPixbuff()
        else:
            ptext = self.book.pages[nr-1].text 
            if self.book.pages[nr-1].tagimage is not None:
                pbuff = self.book.pages[nr-1].tagimage.asGtkPixbuff()
            elif self.book.pages[nr-1].blobimage is not None:
                pbuff = self.book.pages[nr-1].blobimage.asGtkPixbuff()
            else:
                pbuff = self.book.pages[nr-1].image.asGtkPixbuff()

            if self.book.pages[nr-1].objectWords is not None:
                buf.insert_at_cursor("Identifed entities:\n")
                for word in self.book.pages[nr-1].objectWords:
                    buf.insert_at_cursor(word + "\n")
        
        self.wTree.get_widget("textviewPageText").get_buffer().set_text(ptext)
        self.wTree.get_widget("imagePage").set_from_pixbuf(pbuff)
        self.wTree.get_widget("labelPageNr").set_text(str(nr) + " of " + str(self.book.nrOfPages))
        self.page = nr

    def compareBlobs(self, widget):
        compareBox = AiCompareGui.AiCompareGui(self.book)

    def blobTrace(self, widget):
        self.book.findBlobs()
        self.checkButtons()
        self.loadPage(self.page)

    def nlp(self, widget):
        self.book.doNLP()
        self.checkButtons()
        self.loadPage(self.page)

    def em(self, widget):
        self.book.doEM()
        self.checkButtons()
        self.loadPage(self.page)

    def checkButtons(self):
        if self.book is not None:
            if self.book.blobsTraced:
                self.wTree.get_widget("buttonBlobTrace").set_sensitive(False)
                self.wTree.get_widget("buttonCompareBlobs").set_sensitive(True)
            else:
                self.wTree.get_widget("buttonBlobTrace").set_sensitive(True)
                self.wTree.get_widget("buttonCompareBlobs").set_sensitive(False)
            
            if self.book.nlp:
                self.wTree.get_widget("buttonNLP").set_sensitive(False)
            else:
                self.wTree.get_widget("buttonNLP").set_sensitive(True)

            if self.book.blobsTraced and self.book.nlp and not self.book.em:
                self.wTree.get_widget("buttonEM").set_sensitive(True)
            else:
                self.wTree.get_widget("buttonEM").set_sensitive(False)
                
        else:
            self.wTree.get_widget("buttonNextPage").set_sensitive(False)
            self.wTree.get_widget("buttonPrevPage").set_sensitive(False)
            self.wTree.get_widget("buttonBlobTrace").set_sensitive(False)
            self.wTree.get_widget("buttonNLP").set_sensitive(False)
            self.wTree.get_widget("buttonEM").set_sensitive(False)
            self.wTree.get_widget("buttonCompareBlobs").set_sensitive(False)

    def nextPage(self, widget):
        self.loadPage(self.page + 1)

        if self.book.nrOfPages < self.page + 1:
            self.wTree.get_widget("buttonNextPage").set_sensitive(False)
        else:
            self.wTree.get_widget("buttonNextPage").set_sensitive(True)
        if self.page < 1:
            self.wTree.get_widget("buttonPrevPage").set_sensitive(False)
        else:
            self.wTree.get_widget("buttonPrevPage").set_sensitive(True)

    def prevPage(self, widget):
        self.loadPage(self.page - 1)

        if self.book.nrOfPages < self.page + 1:
            self.wTree.get_widget("buttonNextPage").set_sensitive(False)
        else:
            self.wTree.get_widget("buttonNextPage").set_sensitive(True)
        if self.page < 1:
            self.wTree.get_widget("buttonPrevPage").set_sensitive(False)
        else:
            self.wTree.get_widget("buttonPrevPage").set_sensitive(True)
            

if __name__ == "__main__":
    aigui = AiGui()
    if __debug__:
        print "SYS: Starting GUI"
    gtk.main()

